cap log close
log using "$logs/an-descriptives.log", replace

use "$data/commonsamp", clear
	
	// Merge first with SEDA Covs, Gaps, and OCR
	merge 1:1 countyid using "$data/sedagaps", assert(2 3) keep(3) nogen
	merge 1:1 countyid using "$data/seda_covariates_clean", assert(2 3) keep(3) nogen
	merge 1:1 countyid using "$data/1216_countylevel_clean.dta", assert(2 3) keep(3) nogen
	egen rm = rowmiss(*)
	assert rm == 0
	drop rm

	gen pr_bl_iss = iss_bl_all/enr_bl_all
	gen pr_wh_iss = iss_wh_all/enr_wh_all
	gen pr_bl_oos = oos_bl_all/enr_bl_all
	gen pr_wh_oos = oos_wh_all/enr_wh_all	
	
	preserve
	
		collapse (mean) ///
			mn_avg_ol ///
			perblk /*pctenglish1*/ perhsp stutch_all hsflnfl ppexp_inst percharter_all sesall hswhtblk flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			pr_bl_iss pr_wh_iss pr_bl_oos pr_wh_oos enr_bl_all enr_wh_all (count) N=countyid
		renvars *, prefix(mn)
		rename mnN N
		reshape long mn, i(N) j(var) string
		tempfile mean
		save `mean'
	
	restore
	
	
		collapse (sd) ///
			mn_avg_ol ///
			perblk /*pctenglish1*/ perhsp stutch_all hsflnfl ppexp_inst percharter_all sesall hswhtblk flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
			pr_bl_iss pr_wh_iss pr_bl_oos pr_wh_oos enr_bl_all enr_wh_all (count) N=countyid
		renvars *, prefix(sd)
		rename sdN N
		reshape long sd, i(N) j(var) string
		merge 1:1 N var using `mean', assert(3) nogen
	tempfile seda 
	save `seda'
	
// Merge with IAT data	
use "$data/commonsamp", clear	
merge 1:m countyid using "$data/iat", keep(3) 
keep if k12==1
egen rm = rowmiss(implicitbias amind eastasian southasian nathaw black white blackwhite multiother otherun female agecat ed schoolyear)
keep if rm == 0
drop rm

tab agecat, gen(age_)
tab ed, gen(ed_)
tab schoolyear, gen(year_)
gen temp = _n

preserve
	collapse (mean) implicitbias amind eastasian southasian nathaw black white blackwhite multiother otherun female age_* ed_* year_* (count) N=temp
	renvars *, prefix(mn)
	rename mnN N
	reshape long mn, i(N) j(var) string
	tempfile mean
	save `mean'
restore

	collapse (sd) implicitbias amind eastasian southasian nathaw black white blackwhite multiother otherun female age_* ed_* year_* (count) N=temp
	renvars *, prefix(sd)
	rename sdN N
	reshape long sd, i(N) j(var) string
	merge 1:1 N var using `mean', assert(3) nogen
	
append using `seda'
outsheet using "$output/an-descriptives.csv", comma replace

log close


